// pages/my-pages/myShop/myShop.js
const httpUtils = require('../../../utils/httpUtils')
const ui = require('../../../utils/ui')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    user: {},
    dialogKey: '',
    showTextAndTitleWithInput: false,
  },

  inputPhone: function (e) {
    console.log(e.detail.value)
    this.setData({
      'user.phone': e.detail.value
    })
  },
  inputEmail: function (e) {
    console.log(e.detail.value)
    this.setData({
      'user.email': e.detail.value
    })
  },
  radioChange: function(e) {
    let check = e.detail.value;
    console.log(check)
    this.setData({
      'user.genderRadioFlag': check
    })
  },
  inputNickName: function (e) {
    console.log(e.detail.value)
    this.setData({
      'user.nickName': e.detail.value
    })
  },
  getUserInfoFunc: function () {
    //获取用户信息
    let obj = {
      method: 'GET',
      showLoading: false,
      url: 'user/getUserInfo'
    }
    httpUtils.request(obj).then(res => {
      console.log(res.data.message)
      if (res.data.retCode == 0) {
        // console.log('用户信息: ' + res.data.data)
        this.setData({
          user: res.data.data
        })
      } else {
        ui.showToast(res.data.message)
      }
    }).catch(err => {
      ui.showToast('请求失败')
    });
  },

  confirmSaveButton: function () {
    let user = this.data.user;
    let obj = {
      method: 'POST',
      showLoading: true,
      url: 'user/updateUserBase',
      data: user
    }
    httpUtils.request(obj).then(res => {
      if (res.data.retCode == 0) {
        this.getUserInfoFunc()
      } else {
        ui.showToast(res.data.message)
      }
    }).catch(err => {
      ui.showToast('请求失败')
    });
  },

  changeBindButton: function (e) {
    const { key } = e.currentTarget.dataset;
    this.setData({ [key]: true, dialogKey: key });
  },

  closeDialog: function () {
    const { dialogKey } = this.data;
    this.setData({ [dialogKey]: false });
  },

  confirmChangeShop: function(){
    let shopCode = this.data.changeShopCode;
    console.log('点击了确认切换绑定按钮, 店铺编码: ' + shopCode)
    let obj = {
      method: 'GET',
      showLoading: false,
      url: 'myShop/bindShop?shopCode=' + shopCode
    }
    httpUtils.request(obj).then(res => {
      console.log(res.data.message)
      if (res.data.retCode == 0) {
        // console.log('用户信息: ' + res.data.data)
        this.getUserInfoFunc();
      } else {
        ui.showToast(res.data.message)
      }
    }).catch(err => {
      ui.showToast('请求失败')
    });
    const { dialogKey } = this.data;
    this.setData({ [dialogKey]: false });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getUserInfoFunc();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})